Imports DAL
Imports myclasses
Imports System.Configuration

Public Class frmNewUser
    Dim _PersonID As Integer
    Dim dtEmp As New DataTable
    Public intEmpNo As Integer
    Public IsPrivDataSaved As Boolean


    Private Sub frmNewUser_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            'Modified By:-Divya(20071230)
            If isUpdate() Then
                 Select Case BCommon.ShowMsgError(10081, "YNC")
                    Case Windows.Forms.DialogResult.Yes
                        If ValidateData() Then
                            Call SaveData()
                        Else
                            e.Cancel = True
                        End If
                    Case Windows.Forms.DialogResult.Cancel
                        e.Cancel = True
                    Case Windows.Forms.DialogResult.No
                End Select
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnClose_Click")
        End Try
    End Sub

    Private Sub frmNewUser_HelpButtonClicked(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked
        'Rauhl_ITS@20073012 : Add following code display help
        Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "User")
        e.Cancel = True
    End Sub

    Private Sub frmNewUser_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.S And e.Control = True Then
            Call SaveData()
        End If
        'Rauhl_ITS@20073012 : Add following code display help
        If e.KeyCode = Keys.F1 Then
            e.SuppressKeyPress = True
            Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "User")
        End If
    End Sub
    Private Function isUpdate() As Boolean
        Try
            If Trim(txtUserID.Text) <> txtUserID.Tag Or Trim(txtPwd.Text) <> txtPwd.Tag Or _
            Trim(txtConfPwd.Text) <> txtConfPwd.Tag Or Trim(txtEmpID.Text) <> txtEmpID.Tag Then
                Return True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "isUpdate")
        End Try
    End Function
    Private Sub frmNewuser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Call BCommon.SetFormStandards(Me)
            cmbRole.DropDownStyle = ComboBoxStyle.DropDownList
            'Call ClearData()
            Dim intCount As Integer
            dtEmp = BUser.GetUserList()
            If intEmpNo <> 0 Then
                For intCount = 0 To dtEmp.Rows.Count - 1
                    If intEmpNo = dtEmp.Rows(intCount).Item(0) Then
                        txtEmpID.Text = dtEmp.Rows(intCount).Item(1)
                        txtName.Text = dtEmp.Rows(intCount).Item(2)
                        _PersonID = dtEmp.Rows(intCount).Item(3)
                    End If
                Next
            End If
            Call Display_Data()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmNewuser_Load")
        End Try
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If ValidateData() = False Then Exit Sub
        Call SaveData()
        Call Display_Data()
    End Sub

    Private Function setData() As Cuser
        Dim objUser As New Cuser
        Try
            With objUser
                .LoginID = txtUserID.Text
                .Password = txtPwd.Text
                .PersonID = _PersonID
                .RoleId = cmbRole.SelectedValue
                '.OperatorID = txtOperatorID.Text
                If intEmpNo = 0 Then
                    .EmployeeID = fgEmpList.Cols(0).Item(fgEmpList.Row)
                Else
                    .EmployeeID = intEmpNo
                End If
            End With
            Return objUser

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "setData")
            Return Nothing
        End Try

    End Function

    Private Sub Display_Data()
        'Dim dtsTitle As New DataTable
        Dim dtRoles As New DataTable
        Try
            'txtName.Enabled = False
            'txtEmpID.Enabled = False
            'Divya 30/01/2008 : Added for ReadOnly fields
            BCommon.SetTextReadOnly(txtEmpID)
            BCommon.SetTextReadOnly(txtName)
            '>>End
            fgEmpList.Visible = False
            'dtsTitle = DMiscCode.GetTitles()
            dtRoles = BRoles.GetRoles()
            BRoles.SetComboValues(cmbRole, dtRoles)

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "Display_Data")
        End Try
    End Sub

    Private Function ValidateData() As Boolean
        If Trim(txtName.Text) = "" Then
            BCommon.ShowMsgError(10007)
            btnEmp.Focus()
            Return False
        End If
        If Trim(txtUserID.Text) = "" Then
            BCommon.ShowMsgError(10013)
            txtUserID.Focus()
            Return False
        End If
        If Trim(cmbRole.Text) = "" Then
            BCommon.ShowMsgError(10415)
            cmbRole.Focus()
            Return False
        End If
        If btnSave.Text = "&Save" Then
            If Trim(txtPwd.Text) = "" Then
                BCommon.ShowMsgError(10014)
                txtPwd.Focus()
                Return False
            End If
            If Trim(txtConfPwd.Text) = "" Then
                BCommon.ShowMsgError(10092)
                txtConfPwd.Focus()
                Return False
            End If
            If Trim(txtPwd.Text) <> Trim(txtConfPwd.Text) Then
                BCommon.ShowMsgError(10015)
                txtPwd.Text = ""
                txtConfPwd.Text = ""
                txtPwd.Focus()
                Return False
                End
            End If
        End If
        Return True
    End Function

    Private Function SaveData() As Boolean
        Dim objNewuser As New Cuser
        Dim blnResult As Boolean
        objNewuser = setData()
        If btnSave.Text = "&Save" Then
            blnResult = BUser.Save(objNewuser, "S")
            If blnResult Then BCommon.ShowMsgError(3) Else BCommon.ShowMsgError(4)
        Else
            blnResult = BUser.Save(objNewuser, "U")
            If blnResult Then BCommon.ShowMsgError(5) Else BCommon.ShowMsgError(4)
            IsPrivDataSaved = True
        End If
        If blnResult = True Then
            ClearData()
            intEmpNo = 0
            btnEmp.Focus()
            Me.Close()
            'Else
            '    BCommon.ShowMsgError(10017) : txtUserID.Text = "" : txtPwd.Text = "" : txtConfPwd.Text = ""
        End If
        If blnResult Then Call Display_Data()
        Return blnResult

        If blnResult = True Then
            BCommon.ShowMsgError(3)
            Call ClearData()
        Else
            BCommon.ShowMsgError(4)
        End If
    End Function

    Private Sub ClearData()
        txtEmpID.Text = ""
        txtName.Text = ""
        fgEmpList.Visible = False
        txtUserID.Text = ""
        txtPwd.Text = ""
        txtConfPwd.Text = ""

        txtEmpID.Tag = txtEmpID.Text
        txtName.Tag = txtName.Text
        txtUserID.Tag = txtUserID.Text
        txtPwd.Tag = txtPwd.Text
        txtConfPwd.Tag = txtConfPwd.Text
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        'Vrushali........14/04/2007
        Try
            Call ClearData()
            Call EnableControls()
            btnEmp.Focus()
            Display_Data()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnCancel_Click")
        End Try
    End Sub
    ' Code Start                  Vrushali........14/04/2007
    Private Sub btnEmp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmp.Click

        dtEmp = BUser.GetUserList()
        If dtEmp.Rows.Count <= 0 Then
            BCommon.ShowMsgError(10344)
        Else
            With fgEmpList
                If .Visible = True Then
                    .Visible = False
                Else
                    .Visible = True
                    .Focus()
                    .DataSource = dtEmp
                    .Cols(1).Width = .Width * 20 / 100
                    .Cols(2).Width = .Width * 78 / 100
                    .Cols(0).Visible = False
                    .Cols(3).Visible = False
                End If
            End With
        End If
    End Sub

    Private Sub fgEmpList_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles fgEmpList.DoubleClick
        Try
            Call Display_Empoyee()

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgEmpList_DoubleClick")
        End Try
    End Sub

    Private Sub fgEmpList_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles fgEmpList.KeyUp
        Try
            If e.KeyCode = Keys.Enter Then
                Call Display_Empoyee()
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgEmpList_DoubleClick")
        End Try

    End Sub

    Private Sub Display_Empoyee()
        Try
            fgEmpList.Visible = False
            txtEmpID.Text = fgEmpList.Cols(1).Item(fgEmpList.Row)
            txtName.Text = fgEmpList.Cols(2).Item(fgEmpList.Row)
            _PersonID = fgEmpList.Cols(3).Item(fgEmpList.Row)
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "Display_Empoyee")
        End Try
    End Sub
    'Divya 26/10/2007
    Private Sub txtUserID_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtUserID.KeyDown
        Try
            If e.Alt Then Exit Sub
            If e.KeyCode = Keys.Enter Then cmbRole.Focus()
            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True : Exit Sub
                End If
            End If
            If BCommon.ValidateMiscell(e.KeyCode, "USERID") = False Then    'Divya 23/10/2007
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtUserID_KeyDown")
        End Try
    End Sub

    Private Sub txtPwd_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPwd.KeyDown
        Try
            If e.Alt Then Exit Sub
            If e.KeyCode = Keys.Enter Then txtConfPwd.Focus()
            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True : Exit Sub
                End If
            End If
            If BCommon.ValidateMiscell(e.KeyCode, "USERID") = False Then    'Divya 23/10/2007
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtUserID_KeyDown")
        End Try
    End Sub

    Private Sub txtConfPwd_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtConfPwd.KeyDown
        Try
            If e.Alt Then Exit Sub
            If e.KeyCode = Keys.Enter Then btnSave.Focus()
            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True : Exit Sub
                End If
            End If
            If BCommon.ValidateMiscell(e.KeyCode, "USERID") = False Then    'Divya 23/10/2007
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtUserID_KeyDown")
        End Try
    End Sub

    Private Sub cmbRole_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbRole.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then txtPwd.Focus()
            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True : Exit Sub
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmbRole_KeyDown")
        End Try
    End Sub

    Private Sub DisableControls()
        Try
            btnEmp.Enabled = False
            txtUserID.Enabled = False
            txtPwd.Enabled = False
            txtConfPwd.Enabled = False
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisableControls")
        End Try
    End Sub

    Private Sub EnableControls()
        Try
            btnEmp.Enabled = True
            txtUserID.Enabled = True
            txtPwd.Enabled = True
            txtConfPwd.Enabled = True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "EnableControls")
        End Try
    End Sub

    Private Sub ViewData(ByVal EmpID As Integer)

        Dim dtEmpRole As New DataTable
        Try
            dtEmpRole = BRoles.GetEmpRole(EmpID)
            If (dtEmpRole.Rows.Count > 0) Then
                txtEmpID.Text = dtEmpRole.Rows(0).Item(0)
                txtName.Text = dtEmpRole.Rows(0).Item(1)
                txtUserID.Text = dtEmpRole.Rows(0).Item(2)
                cmbRole.Text = dtEmpRole.Rows(0).Item(3)
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ViewData")
        End Try
    End Sub
    Private Sub btnSrch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSrch.Click
        Dim form As New frmMasterBrowse
        Try
            form.strSearch = "User"
            form.StartPosition = FormStartPosition.Manual
            form.Height = MDIPharmacy.MdiScaleHeight
            form.Width = MDIPharmacy.MdiScaleWidth
            Dim pt As New Point
            pt.Y = ((MDIPharmacy.MdiScaleHeight - form.Height) / 2) + MDIPharmacy.pnlHeading.Height + MDIPharmacy.MenuStrip.Height
            pt.X = (MDIPharmacy.MdiScaleWidth - form.Width) / 2
            form.Location = pt
            form.ShowInTaskbar = False
            form.ShowDialog()

            intEmpNo = form.strRecordNo
            If intEmpNo <> "0" Then
                Call DisableControls()
                btnSave.Text = "&Update"
                Call ViewData(intEmpNo)
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSearch_Click")
        End Try
    End Sub
End Class